package com.hanrui.android.activity.message.audit;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.TextView;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.hanrui.android.R;
import com.hanrui.android.activity.message.GoBackActivity;
import com.hanrui.android.library.base.ActionBarBaseActivity;
import com.hanrui.android.fragment.message.LcFragemnt;
import com.hanrui.android.fragment.message.audit.businesstravel.BusinessTravelBizFragment;
import com.hanrui.android.fragment.message.audit.businesstravel.BusinessTravelDetailFragment;
import com.hanrui.android.library.widget.TitleBar;
import com.hanrui.android.widget.bottomnavigationbar.BottomApprovalBar;

import java.util.Stack;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 出差审批
 */
public class BusinessTravelActivity extends ActionBarBaseActivity {

    String taskId,procInsId,bizId;
    @BindView(R.id.bottomBar)
    BottomApprovalBar bottomApprovalBar;

    BusinessTravelDetailFragment businessTravelDetailFragment;
    LcFragemnt lcFragemnt;
    BusinessTravelBizFragment businessTravelBizFragment;

    private Stack<Fragment> stack = new Stack<>();
    FragmentTransaction manager;

    TextView rightTitle;

    @Override
    protected void initInjector() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_approval);

        unbinder = ButterKnife.bind(this);

        mTitleBar.setTitle("出差审批");

        rightTitle = (TextView) mTitleBar.addAction(new TitleBar.TextAction("退回") {
            @Override
            public void performAction(View view) {

                GoBackActivity.start(BusinessTravelActivity.this,taskId,procInsId,bizId);

            }
        });

        rightTitle.setTextColor(ContextCompat.getColor(this,R.color.white));
        rightTitle.setVisibility(View.GONE);

        taskId = getIntent().getStringExtra("taskId");
        procInsId = getIntent().getStringExtra("procInsId");
        bizId = getIntent().getStringExtra("bizId");


        initFragment();
        initBottomNav();
        changeFragment(0);

    }

    private void initFragment(){
        manager = getSupportFragmentManager().beginTransaction();

        businessTravelDetailFragment = BusinessTravelDetailFragment.newInstance(taskId,procInsId,bizId);
        lcFragemnt = LcFragemnt.newInstance(taskId,procInsId,bizId);
        businessTravelBizFragment = BusinessTravelBizFragment.newInstance(taskId,procInsId,bizId);

        manager.add(R.id.contentContainer,businessTravelDetailFragment);
        manager.add(R.id.contentContainer,lcFragemnt);
        manager.add(R.id.contentContainer,businessTravelBizFragment);

        stack.add(businessTravelDetailFragment);
        stack.add(lcFragemnt);
        stack.add(businessTravelBizFragment);

        manager.commit();
    }

    /*
        初始化底部导航切换事件
     */
    private void initBottomNav(){

        bottomApprovalBar.setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener() {
            @Override
            public void onTabSelected(int position) {
                switch (position) {
                    case 0:
                        mTitleBar.setTitle("出差审批");
                        rightTitle.setVisibility(View.GONE);
                        break;
                    case 1:
                        mTitleBar.setTitle("流程图");
                        rightTitle.setVisibility(View.GONE);
                        break;
                    case 2:
                        mTitleBar.setTitle("流程审批");
                        rightTitle.setVisibility(View.VISIBLE);
                        break;

                		default:
                			break;
                		}
                changeFragment(position);
            }

            @Override
            public void onTabUnselected(int position) {

            }

            @Override
            public void onTabReselected(int position) {

            }
        });
    }


    /*
        切换Tab，切换对应的Fragment
     */
    private void changeFragment(int position) {
        manager = getSupportFragmentManager().beginTransaction();
        for (Fragment fragment: stack){
            manager.hide(fragment);
        }
        manager.show(stack.get(position));
        manager.commit();
    }

    public static void start(Context context, String taskId, String procInsId, String bizId){

        Intent intent = new Intent(context,BusinessTravelActivity.class);
        intent.putExtra("taskId",taskId);
        intent.putExtra("procInsId",procInsId);
        intent.putExtra("bizId",bizId);
        context.startActivity(intent);
    }
}
